<?php

class Applicants extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'url', 'date'));
        $this->load->library(array('session', 'form_validation'));

        //validate login

        if ($this->session->userdata("username") == FALSE) {
            $func = $this->uri->segment(2);
            if ($func == "updateApplicant" || $func == "profile") {
                redirect("applicants/openLogin/You must login first");
            }
        }

        $this->load->model("applicants_model", "", TRUE);
        $this->load->model("consolidate_model", "", TRUE);
        $this->load->model("settings/log_model", "", TRUE);
    }

    function newApplicant() {
        $data["title"] = "- Register";
        $this->load->view('templates/header', $data);
        $data["departments"] = $this->consolidate_model->getDepartments();
        $this->load->view("applicants/newApplicant", $data);
        $this->load->view('templates/footer');
    }

    function saveApplicant() {
        $this->form_validation->set_rules('surname', 'Surname', 'required');
        $this->form_validation->set_rules('first_name', 'First Name', 'required');
        //  $this->form_validation->set_rules('other_names', 'Other Names', 'required');
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|matches[password2]|md5');
        $this->form_validation->set_rules('password2', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('phone_number', 'Phone Number', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('idno', 'ID No', 'required');
        $this->form_validation->set_rules('date_of_employment', 'Date of employment', 'required');
        $this->form_validation->set_rules('pay_roll_no', 'Pay roll No', 'required');
        $this->form_validation->set_rules('dept_id', 'Depeartment', 'required');
        if ($this->form_validation->run() == FALSE) {
            $data["title"] = "- Registering";
            $this->load->view('templates/header', $data);
            $data["departments"] = $this->consolidate_model->getDepartments();
            $this->load->view("applicants/newApplicant", $data);
            $this->load->view('templates/footer');
        } else {
            $this->applicants_model->newApplicant();
            $this->log_model->newLog("Successfully registered", "User", $_POST["surname"] . " " . $_POST["first_name"] . " " . $_POST["other_names"] . " was registered using username " . $_POST["username"]);
            redirect("applicants/openLogin/Successfully registered");
        }
    }

    function updateApplicant() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('surname', 'Surname', 'required');
        $this->form_validation->set_rules('first_name', 'First Name', 'required');
        $this->form_validation->set_rules('phone_number', 'Phone Number', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('idno', 'ID No', 'required');
        $this->form_validation->set_rules('dept_id', 'Depeartment', 'required');
        if ($this->form_validation->run() == FALSE) {
            redirect("applicants/profile/update failed, " . strip_tags(str_replace("\n", " ", validation_errors())));
        } else {
            $data["departments"] = $this->applicants_model->updateApplicant($this->session->userdata("username"));
            $this->log_model->newLog("Profile updated", "User", $_POST["surname"] . " " . $_POST["first_name"] . " " . $_POST["other_names"] . " updated their profile", $this->session->userdata("username"));
            redirect("applicants/profile/Profile updated");
        }
    }

    function _username_check($str) {
        echo $str;
        if ($str == 'simon') {
            $this->form_validation->set_message('_username_check', '%s field can not be the word "simon"');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    function openLogin($message="", $errors="") {
        $data['message'] = urldecode($message);
        $data['errors'] = urldecode($errors);
//        $data['message']=$this->session->userdata("message");
        $data["title"] = "- Login";
        $this->load->view('templates/header', $data);
        $this->load->view("applicants/login", $data);
        $this->load->view('templates/footer');
    }

    function login() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required|md5');
        $this->load->view('templates/header', $data);
        if ($this->form_validation->run() == FALSE) {
            redirect("applicants/openLogin/Retry/All fields must be given");
        } else {
            if ($this->applicants_model->validateLogin()) {
                $data = array("username" => $this->input->post("username"));
                $this->session->set_userdata($data);
                $this->log_model->newLog("User Login", "User", $_POST["username"] . " logged in", $this->session->userdata("username"));
                redirect("mis");
            } else {
                redirect("applicants/openLogin/Retry/Wrong username or password");
            }
        }
    }

    function profile($message="") {
        $data["title"] = "- Profile";
        $this->load->view('templates/header', $data);
        $data["profile"] = $this->applicants_model->getProfile($this->session->userdata("username"));
        $data["departments"] = $this->consolidate_model->getDepartments();
        $data['message'] = urldecode($message);
        $this->load->view("applicants/profile", $data);
        $this->load->view('templates/footer');
    }

    function logout() {
        $this->session->sess_destroy();
        redirect("welcome/index/no");
    }

}

?>
